import { message } from 'antd';
import { getBalanceHistory, userLevelMnglog } from './service';

import { ModelType } from '@/common-typings.d';
import types from '@/const/constType';

const { CODE } = types;

const Model: ModelType = {
  namespace: 'search',
  state: {
    // 交易明细列表
    trsdeDetailsList: [],
    // 层级调整列表
    userLevelMnglogList: [],
  },

  effects: {
    *getBalanceHistory({ payload, fn }, { call, put }) {
      const response = yield call(getBalanceHistory, payload);
      if (response.code === CODE) {
        yield put({ type: 'setBalanceHistoryList', payload: response });
        if (fn) {
          fn(response.data);
        }
      }
    },
    *userLevelMnglog({ payload, fn }, { call, put }) {
      const response = yield call(userLevelMnglog, payload);
      if (response.code === CODE) {
        yield put({ type: 'setUserLevelMnglogList', payload: response });
        if (fn) {
          fn(response.data);
        }
      }
    },
  },

  reducers: {
    setBalanceHistoryList(state, { payload }) {
      const newState = JSON.parse(JSON.stringify(state));
      newState.trsdeDetailsList = payload.data;
      return {
        ...state,
        ...newState,
      };
    },
    setUserLevelMnglogList(state, { payload }) {
      const newState = JSON.parse(JSON.stringify(state));
      newState.userLevelMnglogList = payload.data;
      return {
        ...state,
        ...newState,
      };
    },
  },
};

export default Model;
